<template>
  <div class="app-container">
    <div style="margin-left: -29px;">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="别名" prop="aliasName">
        <el-input
          v-model="queryParams.aliasName"
          placeholder="请输入别名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品类型" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择产品类型" clearable size="small">
          <el-option label="种植业" value="种植业" />
           <el-option label="畜牧业" value="畜牧业" />
           <el-option label="其他" value="其他" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </div>
    
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['TProductScheme:productScheme:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['TProductScheme:productScheme:export']"
        >导出</el-button>
      </el-col>
    </el-row> -->
    <div class="shuxing">
       <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入公司名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            class="trees"
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
    </div>
    <el-table v-loading="loading" :data="productSchemeList" @selection-change="handleSelectionChange" class="otable">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品代码" align="center" prop="objId" />
      <el-table-column label="别名" align="center" prop="aliasName" />
      <el-table-column label="英文名称" align="center" prop="englishName" />
     <!--  <el-table-column label="英文名称" align="center" prop="lvl" /> -->
      <el-table-column label="对应 GB/T 7635.1 代码" align="center" prop="correspondingCode" />
      <el-table-column label="是否行业" align="center" prop="isPackage" />
      <el-table-column label="英文名称" align="center" prop="lft" />
   <!--    <el-table-column label="英文名称" align="center" prop="parentId" /> -->
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品类型" align="center" prop="productType" />
     <!--  <el-table-column label="英文名称" align="center" prop="rgt" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['TProductScheme:productScheme:edit']"
          >修改</el-button>
         <!--  <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['TProductScheme:productScheme:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
       class="page"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品规格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="别名" prop="aliasName">
          <el-input v-model="form.aliasName" placeholder="请输入别名" />
        </el-form-item>
        <el-form-item label="英文名称" prop="englishName">
          <el-input v-model="form.englishName" placeholder="请输入英文名称" />
        </el-form-item>
        <!-- <el-form-item label="英文名称" prop="lvl">
          <el-input v-model="form.lvl" placeholder="请输入英文名称" />
        </el-form-item> -->
        <el-form-item label="对应 GB/T 7635.1 代码" prop="correspondingCode">
          <el-input v-model="form.correspondingCode" placeholder="请输入对应 GB/T 7635.1 代码"/>
        </el-form-item>
        <el-form-item label="是否行业产品" prop="isPackage">
         <el-select v-model="queryParams.isPackage" placeholder="请选择行业产品" clearable size="small" style="width: 400px">
          <el-option label="是" value="是" />
           <el-option label="否" value="否" />
        </el-select>
        </el-form-item>
        
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品类型">
          <el-select v-model="form.productType" placeholder="请选择产品类型" style="width: 400px">
            <el-option label="种植业" value="种植业" />
              <el-option label="畜牧业" value="畜牧业" />
                <el-option label="其他" value="其他" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listProductScheme, getProductScheme, delProductScheme, addProductScheme, updateProductScheme, exportProductScheme, listProductTree } from "@/api/TProductScheme/productScheme";

import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "ProductScheme",
  components: { Treeselect },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  data() {
    return {
      // 公司名称
      deptName: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
       // 部门树选项
      deptOptions: undefined,
      // 总条数
      total: 0,
      // 产品规格表格数据
      productSchemeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        objId: undefined,
        aliasName: undefined,
        englishName: undefined,
        lvl: undefined,
        correspondingCode: undefined,
        isPackage: undefined,
        lft: undefined,
        parentId: undefined,
        productName: undefined,
        productType: undefined,
        rgt: undefined
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getPrductTree(); 
  },
  methods: {
    /** 查询产品规格列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams);
      listProductScheme(this.queryParams).then(response => {
        this.productSchemeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
    
      this.queryParams.objId ="0"+ data.id;
      this.getList();
    },
    getPrductTree() {
      this.loading = true;
      listProductTree().then(response => {
	        this.deptOptions = this.handleTree(response.data, "id");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        objId: undefined,
        aliasName: undefined,
        englishName: undefined,
        lvl: undefined,
        correspondingCode: undefined,
        isPackage: undefined,
        lft: undefined,
        parentId: undefined,
        productName: undefined,
        productType: undefined,
        rgt: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.objId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品规格";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const objId = row.objId || this.ids
      getProductScheme(objId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品规格";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.objId != undefined) {
            updateProductScheme(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addProductScheme(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const objIds = row.objId || this.ids;
      this.$confirm('是否确认删除产品规格编号为"' + objIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProductScheme(objIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品规格数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProductScheme(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    }
  }
};
</script>
<style lang="scss" scoped>
 .shuxing{
   width: 10%;
   min-width: 200px;
   height: 550px;
   overflow-y: auto;;
   float: left;
  //  background-color: aqua;
   margin-right: 2%;
 }
 .otable{
   width: 84%;
  //  float: left;
 }
 .shuxing .el-col{
   width: 100%;
 }
 .page{
   width: 84%;
   float: left;
 }
</style>